Introduction to Bit¶

Learning Outcomes¶

  • pip install byubit
  • Introduction to Bit
    • move, left, right, paint
  • Overview of a Bit script
    • from byubit import Bit
    • @Bit.worlds
    • call the function!
  • Syntax
    • indentation
    • quotes on strings
    • () on function calls
    • Casing
  • Running Bit scripts
    • Example in PyCharm
    • Demonstrate how to step through the code with the Bit UI
    • Demonstrate what it looks like when something is incorrect

We can install code that other people wrote (called a "package") using the pip command in our terminal.

We'll use pip to install byubit:

conda run -n cs110 pip install byubit

Once we've installed a package, we can use it in a script.

Bit¶

Bit lives in a grid world, like a chessboard.

He can do four actions: move, left, right, and paint.

hello_bit.py¶

from byubit import Bit

This means that byubit (what we pip installed) has a thing called Bit, and we want to use it.

def move_around(bit):
    ...

Here we define a function that will tell Bit what to do.

In this case, we named the function parameter bit, but we could have named it robot or Sally or cosmo.

e.g.

def move_around(cosmo):
    cosmo.move()
    ...

We'll talk more about functions in a few lectures.

@Bit.empty_world(5,3)

This means that we want Bit (the thing we imported from byubit) to run our function.

In this case, Bit will create an empty world that is 5 spaces wide and 3 spaces tall.

When Bit starts in an empty world, it always starts in the bottom-left corner facing right.

bit.move()
    bit.paint("red")

These lines are the instructions we have for bit.

bit.move is a function. Including () at the end means we want to call that function.

Calling bit.move() makes the bit move one space in the direction it is facing.

bit.paint is a function. Including ("red") at the end means we want to call that function with "red" as a parameter.

Calling bit.paint("red") will paint the current space the specified color.

Valid colors are "red", "blue", and "green". (Notice the quotes ")

move_around(Bit.new_bit)

This line is where we tell python we want to run the move_around function we defined.

The def line is where we defined the recipe, and the move_around(Bit.new_bit) line is where we tell it to actually bake the cake.

Bit.new_bit is the bit that will explore the empty world we requested in @Bit.empty_world(5,3)

Indentation Matters!

def move_around(bit):
    bit.move()
    bit.move()
    bit.paint("red")
####

You'll notice that the lines inside the function are indented 4 spaces. This matters.

Be sure to indent the code that belongs to a function. Later, we'll talk more about this, but this is enough to get you started.

if __name__ == '__main__':

We'll learn more about what this means later. For now, include it before the line where you call your primary function.

@Bit.empty_world(5, 3)
def move_around(bit):  # <-- Call this function...
    ...

if __name__ == '__main__':
    move_around(Bit.new_bit)  # ...down here!

What picture will move_around.py produce?

move_around.py¶

The first, prev, next, and last buttons let you step through your code line by line.

move_around.py¶

What code would you write to produce the following picture?

Sketch out your strategy on a piece of paper.

No description has been provided for this image

What picture does no_cake.py create?

no_cake.py¶

What picture does get_moving.py create?

get_moving.py¶

Was this what you expected?

When you type the name of a fuction, but don't include the () at the end, it's like saying:

Hey computer, did you know there is a function named bit.move?

And the computer says

Yep.

When you include the (), it's like saying:

Hey computer, run the function named bit.move

What picture does go_the_distance.py create?

go_the_distance.py¶

When you try to move the bit to an invalid space, you get an error.

What picture does colorful.py create?

colorful.py¶

bit.paint requires one argument: the 'color'.

Remember the valid values are:

  • "red"
  • "blue"
  • "green"

You can also use single quotes:

  • 'red'
  • 'blue'
  • 'green'

What picture does red.py create?

red.py¶

Remember the quotes!

What picture does big_move.py produce?

big_move.py¶

The casing (i.e. "upper-case" or "lower-case") matters!

Paint is not the same as paint

You can also run Bit in predefined worlds.

For example, 'grassy_field' starts Bit in a world like this:

No description has been provided for this image

And it expects that by the time your function finishes, the world will look like this:

No description has been provided for this image

grassy_field.py¶

You can see that our function didn't quite deliver on the expected result.

What code could we use to finish the picture?

Key Ideas¶

  • from byubit import Bit let's us use Bit in our script
  • bit.move, bit.left, bit.right, and bit.paint are functions that make bit do something
    • But remember the ()!
    • bit.paint needs a color: 'red', 'blue', 'green', e.g. bit.paint('red')
  • @Bit.worlds takes a string that indicates a predefined Bit world to use
  • Be sure to call your main function (or nothing happens!)
  • You can step through the action one step at a time